﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin
{
	/// <summary>
	/// 导航的基类。
	/// </summary>
	public class BaseNav : Hxsoft.AppV1.Page
	{
		protected Literal CtrMainNavHtml;

		protected virtual string ViewAutoBuildLogUrl
		{
			get
			{
				string url = null;
				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					url = "~/CachePageManage/ViewAutoBuildLog.aspx";
				else
					url = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../CachePageManage/ViewAutoBuildLog.aspx&SiteIDQueryStringField=SiteId");
				return this.ResolveUrl(url);
			}
		}

		protected virtual void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				LoadMainNav();
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUrl, IncludeFileType.JS);
			RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUiUrl, IncludeFileType.JS);
			RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUiCssUrl, IncludeFileType.CSS);
			
			base.OnPreRender(e);
		}

		#region LoadItem
		/// <summary>
		/// 加载文章管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Article(TreeNodeCollection nodes)
		{
			if (Articles.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "文章管理";
				node.NavigateUrl = "~/ArticleManage/ArticleList.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载发布管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_ModuleArticle(TreeNodeCollection nodes)
		{
			if (ModuleArticles.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "发布管理";
				node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("../ArticleManage/ModuleNavigation.aspx?LinkUrl=../ArticleManage/ReleaseList.aspx&ModuleIDQueryStringField=ModuleID");
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载评论管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Comment(TreeNodeCollection nodes)
		{
			if (Comments.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "评论管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/CommentManage/CommentList.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../CommentManage/CommentList.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载留言管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_MessageBoard(TreeNodeCollection nodes)
		{
			if (MessageBoards.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "留言管理";
				node.NavigateUrl = "~/MessageBoardManage/MessageBoardList.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载页面块编辑菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_PageBlockEdit(TreeNodeCollection nodes)
		{
			if (PageBlocks.GetEditListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "页面块编辑";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/PageBlockManage/EditList.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../PageBlockManage/EditList.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载链接管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Link(TreeNodeCollection nodes)
		{
			if (Links.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "链接管理";
				node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("../LinkManage/LinkClassNavigation.aspx?LinkUrl=../LinkManage/Default.aspx&ClassIDQueryStringField=ClassID");
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载调查管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Vote(TreeNodeCollection nodes)
		{
			if (Votes.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "投票管理";
				node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("../VoteManage/VoteClassNavigation.aspx?LinkUrl=../VoteManage/VoteList.aspx&ClassIDQueryStringField=ClassID");
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载调查类别管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_VoteType(TreeNodeCollection nodes)
		{
			if (VoteClasses.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "投票类别管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/VoteClassManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../VoteClassManage/Default.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载站点管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Site(TreeNodeCollection nodes)
		{
			if (Sites.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "站点管理";
				node.NavigateUrl = "~/SiteManage/Default.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载栏目管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Module(TreeNodeCollection nodes)
		{
			if (Modules.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "模块管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/ModuleManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../ModuleManage/Default.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载字典类别管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_DicClass(TreeNodeCollection nodes)
		{
			if (DicClasses.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "字典管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/DicClassManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../DicClassManage/Default.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载信息项资源管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_InfoItem(TreeNodeCollection nodes)
		{
			if (InfoItems.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "资源管理";
				node.NavigateUrl = "~/ResourceManage/InfoItem/Default.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载链接类别管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_LinkClass(TreeNodeCollection nodes)
		{
			if (LinkClasses.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "链接类别管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/LinkClassManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../LinkClassManage/Default.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载页面块管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_PageBlock(TreeNodeCollection nodes)
		{
			if (PageBlocks.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "页面块管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/PageBlockManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../PageBlockManage/Default.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载用户管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Users(TreeNodeCollection nodes)
		{
			if (Users.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "用户管理";
				node.NavigateUrl = "~/UserManage/Default.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载角色管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Roles(TreeNodeCollection nodes)
		{
			if (Roles.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "角色管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/RoleManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../RoleManage/Default.aspx&SiteIDQueryStringField=SiteID");
				
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载缓存页管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_CachePages(TreeNodeCollection nodes)
		{
			if (CachePages.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "缓存页管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/CachePageManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../CachePageManage/Default.aspx&SiteIDQueryStringField=SiteId");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载缓存通知管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_CacheDependency(TreeNodeCollection nodes)
		{
			if (SqlCacheDependency.GetManageRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "缓存通知管理";
				node.NavigateUrl = "~/CacheManage/RegisterSqlCacheDependency.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载系统初始化管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_SystemInit(TreeNodeCollection nodes)
		{
			if (Hxsoft.AppV1.Module.SystemInit.GetInitRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "系统初始化";
				node.NavigateUrl = "~/SystemInit/Default.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载修改密码菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_ModifyPassword(TreeNodeCollection nodes)
		{
			User user = Users.CurrentUser;
			if (user == null) return;
			if (Users.GetUpdateRight(user))
			{
				TreeNode node = new TreeNode();
				node.Text = "修改密码";
				node.NavigateUrl = "~/UserManage/ModifyPassword.aspx";
				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载反馈管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_FeedBack(TreeNodeCollection nodes)
		{
			if (Articles.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "反馈管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/FeedBackManage/FeedBackList.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../FeedBackManage/FeedBackList.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}

		/// <summary>
		/// 加载专题管理菜单项。
		/// </summary>
		/// <param name="nodes">加入的集合。</param>
		protected void LoadItem_Special(TreeNodeCollection nodes)
		{
			if (Specials.GetManageListRight())
			{
				TreeNode node = new TreeNode();
				node.Text = "专题管理";

				int? siteId = App.Context.CurrentSiteId;
				if (siteId.HasValue)
					node.NavigateUrl = "~/SpecialManage/Default.aspx";
				else
					node.NavigateUrl = "~/CommonPage/LeftNavigation.aspx?NavigationPage=" + Server.UrlEncode("SiteNavigation.aspx?LinkUrl=../SpecialManage/Default.aspx&SiteIDQueryStringField=SiteID");

				node.Target = "Main";

				nodes.Add(node);
			}
		}
		#endregion


		#region LoadGroup
		/// <summary>
		/// 获取内容管理项节点集合。
		/// </summary>
		/// <returns>内容管理项节点集合。</returns>
		protected virtual TreeNodeCollection GetContentManageNodes()
		{
			TreeNodeCollection nodes = new TreeNodeCollection();

			LoadItem_Article(nodes);
			LoadItem_ModuleArticle(nodes);
			LoadItem_Comment(nodes);
			LoadItem_MessageBoard(nodes);
			LoadItem_PageBlockEdit(nodes);
			LoadItem_Link(nodes);
			LoadItem_Vote(nodes);
			LoadItem_FeedBack(nodes);
			LoadItem_Special(nodes);

			return nodes;
		}

		/// <summary>
		/// 加载内容管理组。
		/// </summary>
		protected void LoadGroup_ContentManage()
		{
			TreeNodeCollection nodes = GetContentManageNodes();

			if (nodes.Count > 0)
			{
				CtrMainNavHtml.Text += @"<div><h3><a href=""#"">内容管理</a></h3><div><ul>";

				foreach (TreeNode node in nodes)
					CtrMainNavHtml.Text += @"<li><a href=""" + this.ResolveUrl(node.NavigateUrl) + @""" target=""" + node.Target + @""">" + node.Text + "</a></li>";

				CtrMainNavHtml.Text += @"</ul></div></div>";
			}
		}

		/// <summary>
		/// 获取系统管理项节点集合。
		/// </summary>
		/// <returns>系统管理项节点集合。</returns>
		protected virtual TreeNodeCollection GetSystemManageNodes()
		{
			TreeNodeCollection nodes = new TreeNodeCollection();

			LoadItem_Site(nodes);
			LoadItem_Module(nodes);
			LoadItem_DicClass(nodes);
			LoadItem_InfoItem(nodes);
			LoadItem_LinkClass(nodes);
			LoadItem_PageBlock(nodes);
			LoadItem_VoteType(nodes);
			LoadItem_Users(nodes);
			LoadItem_Roles(nodes);
			LoadItem_CachePages(nodes);
			LoadItem_CacheDependency(nodes);
			LoadItem_SystemInit(nodes);
			LoadItem_ModifyPassword(nodes);

			return nodes;
		}

		/// <summary>
		/// 加载系统管理组。
		/// </summary>
		protected void LoadGroup_SystemManage()
		{
			TreeNodeCollection nodes = GetSystemManageNodes();

			if (nodes.Count > 0)
			{
				CtrMainNavHtml.Text += @"<div><h3><a href=""#"">系统管理</a></h3><div><ul>";

				foreach (TreeNode node in nodes)
					CtrMainNavHtml.Text += @"<li><a href=""" + this.ResolveUrl(node.NavigateUrl) + @""" target=""" + node.Target + @""">" + node.Text + "</a></li>";

				CtrMainNavHtml.Text += @"</ul></div></div>";
			}
		}
		#endregion


		/// <summary>
		/// 加载主菜单。
		/// </summary>
		protected virtual void LoadMainNav()
		{
			LoadGroup_ContentManage();
			LoadGroup_SystemManage();
		}
	}
}
